//
//  HSAFNetworkManager.m
//  HSTS
//
//  Created by simon on 2017/1/24.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "HSAFNetworkManager.h"
#import "AppDelegate.h"
#import "JSONKit.h"

static NSString * const APPBASEURL = BASE_URL;
static NSString * const APPUPLOADURL = @"http://222.45.42.120:60117/nocshs/uploadFile.do";

static AFHTTPSessionManager *httpClient = nil;

@implementation HSAFNetworkManager
+ (instancetype)sharedClient {
    static HSAFNetworkManager *_networkManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _networkManager = [[HSAFNetworkManager alloc] init];
    });
    
    return _networkManager;
}


- (id)init
{
    self = [super init];
    if(self){
        httpClient = [AFHTTPSessionManager manager];
//        httpClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
//        httpClient = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:APPBASEURL]];
//        httpClient.requestSerializer =  [AFJSONRequestSerializer serializer];
//        httpClient.responseSerializer = [AFHTTPResponseSerializer serializer];
//        httpClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    }
    return self;
}



- (void)postPath:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(HSObjectRequestSuccessWithFinishedBlock)success
     serverError:(HSObjectRequestFailedWithFinishedBlock)serverFailure
    networkError:(HSObjectRequestFailedWithFinishedBlock)netFailure

{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
    [SVProgressHUD showWithStatus:@"加载中"];
    [httpClient POST:HSLoadDataUrl(path)  parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        NSError* err = nil;
        HSBasedResponseModel *basedResponse = [[HSBasedResponseModel alloc] initWithDictionary:responseObject error:&err];
        HSLog(@"basedResponse:%@",basedResponse);
            success(task,basedResponse);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"网络不给力"];
        //         [SVProgressHUD dismissWithDelay:2.0];
        netFailure(task,error);
    }];
    
}

- (void)postPathWithoutSVProgressHud:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(HSObjectRequestSuccessWithFinishedBlock)success
     serverError:(HSObjectRequestFailedWithFinishedBlock)serverFailure
    networkError:(HSObjectRequestFailedWithFinishedBlock)netFailure{
    [httpClient POST:HSLoadDataUrl(path)  parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSError* err = nil;
        HSBasedResponseModel *basedResponse = [[HSBasedResponseModel alloc] initWithDictionary:responseObject error:&err];
        HSLog(@"basedResponse:%@",basedResponse);
        success(task,basedResponse);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        netFailure(task,error);
    }];

}

-(void)uploadBinaryFile:(NSData*)data
               fileName:(NSString*)fileName
             parameters:(NSDictionary *)parameters
               mimeType:(NSString*)mimeType
                success:(HSObjectUploadSuccessWithFinishedBlock)success
                  error:(HSObjectUploadErrorWithFinishedBlock)failed;
{
    httpClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil];
    [httpClient POST:APPUPLOADURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:mimeType];
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        HSLog(@"Success: %@", responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSError* err = nil;
        HSBasedResponseModel *basedResponse = [[HSBasedResponseModel alloc] initWithString:responseStr error:&err];
        success(basedResponse);
    } failure:^(NSURLSessionDataTask * task, NSError *error) {
        HSLog(@"Error: %@", error);
        failed(error);
    }];
    
    
}

-(BOOL)isAFNETRequesting{
    BOOL isRequsting = NO;
    if (httpClient.operationQueue.operations.count > 0) {
        isRequsting = YES;
    }
    return isRequsting;
}

@end
